<template>
  <div class="register-page">
    <div class="body">
      <div class="form-wrap">
        <el-form label-position="top" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email" placeholder="请输入邮箱"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="name">
            <el-input v-model="ruleForm.name" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSubmit">立即创建</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/login'

export default {
  name: 'register',
  data() {
    return {
      ruleForm: {
        email: '',
        name: '',
        password: ''
      },
      rules: {
        email: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    /**
     * 提交
     */
    handleSubmit() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          let res = await register(this.ruleForm)
          let { code, msg } = res
          if (code === 0) {
            this.$message.success(msg)
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import './index.scss';
</style>
